<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH. 'controllers/admin/ad_layout.php');

/**
 * @author Nguyen van chi (chinguyen.phpdev@gmail.com)
 * @copyright 2014 - OtcSoft
 */
class Menu extends Ad_layout 
{
	public function __construct()
	{
		parent::__construct();
	}
	
	
	/**
	 * Quản trị 
	 */
	public function index()
	{
		$tabel = "tbl_menu";
		$results = $this->utility_model->select($tabel);
		$this->_data['main_container'] = $this->load->view('admin/menu/index.phtml', array(
			'results' => $results
		), TRUE);
		$this->load->view('admin/ad_layout_view.phtml', $this->_data);
	} 
	
	
	
	/**
	 * Them moi 
	 */
	public function add()
	{
		$this->form_validation->set_rules('menu_name', 'Menu', 'required');
		$this->form_validation->set_message('required', 'Chưa nhập tên %s.');
		
		if($this->form_validation->run() == FALSE){
				$this->_data['main_container'] = $this->load->view('admin/menu/add.phtml', '', TRUE);
				$this->load->view('admin/ad_layout_view.phtml', $this->_data);
		}else{
			$this->save();
		}
	}
	
	
	/**
	 * Lưu
	 */
	public function save()
	{
		$table = 'tbl_menu';
		$data = array(
			'menu_name' => $this->input->post('menu_name'),
			'menu_type' => $this->input->post('menu_type'),
			'menu_type1' => $this->input->post('menu_type1'),
			'description' => $this->input->post('description'),
			'status' => $this->input->post('status'),
			'created' => date('Y-m-d H:i:s'),
		);
		$result = $this->utility_model->insert($table , $data);
		if($result){
			redirect('admin/me-nu');
		}else {
			log_message('error', 'Some variable did not contain a value.');
		}
	}
	
	
	/**
	 * sua
	 */
	public function edit()
	{
		$id=  $this->uri->segment(4);
		$table = 'tbl_menu';
		$where = 'menu_id';
		$detail = $this->utility_model->getDetail($table, $where, $id);
		$this->form_validation->set_rules('menu_name', 'Menu', 'required');
		$this->form_validation->set_message('required', 'Chưa nhập tên %s.');
		if($this->form_validation->run() == FALSE){
			$this->_data['main_container'] = $this->load->view('admin/menu/edit.phtml', array(
					'detail' => $detail,
			), true);
			$this->load->view('admin/ad_layout_view.phtml', $this->_data);
		}else{
			$this->updated();
		}
	}
	
	
	/**
	 * updated
	 */
	public function updated()
	{
		$baseurl = base_url();
		$id = $this->input->post('menu_id');
		$where = 'menu_id';
		$table = 'tbl_menu';
		$data = array(
				'menu_name' => $this->input->post('menu_name'),
				'menu_type' => $this->input->post('menu_type'),
				'menu_type1' => $this->input->post('menu_type1'),
				'description' => $this->input->post('description'),
				'status' => $this->input->post('status'),
				'updated' => date('Y-m-d H:i:s'),
		);
		$result = $this->utility_model->updated($where, $id, $table, $data);
		if($result){
			echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
			<script>
			alert('Bạn đã cập nhật thành công');
			window.open('{$baseurl}admin/me-nu.html','_self');
			</script>";
		}else {
		log_message('error', 'Some variable did not contain a value.');
		}
	}
	
	
	/**
	* Delete
	*/
	public function delete()
	{
		$where = 'menu_id';
		$table = 'tbl_menu';
		$baseurl = base_url();
		$id_list_str = $this->input->post('id_list');
		$id_dels = explode(",",$id_list_str);
		foreach($id_dels as $id_del){
			$result = $this->utility_model->delete($table, $where , $id_del);
		}
		if($result){
		echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
			<script>
			alert('Bạn đã xóa thành công');
			window.open('{$baseurl}admin/me-nu.html','_self');
			</script>";
		}else {
		log_message('error', 'Some variable did not contain a value.');
	}
	}
	
	
}